package com.ruoyi.project.system.partner.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uniquekey.IdGererateFactory;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.partner.domain.Partner;
import com.ruoyi.project.system.partner.service.IPartnerService;
import com.ruoyi.project.system.relevantimg.domain.RelevantImg;
import com.ruoyi.project.system.relevantimg.service.IRelevantImgService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 合作伙伴Controller
 * 
 * @author LCL
 * @date 2020-12-10
 */
@Controller
@RequestMapping("/system/partner")
public class PartnerController extends BaseController
{
    private String prefix = "system/partner";

    @Autowired
    private IPartnerService partnerService;
    @Autowired
    private IdGererateFactory idGererateFactory;
    @Autowired
    private IRelevantImgService relevantImgService;

    @RequiresPermissions("system:partner:view")
    @GetMapping()
    public String partner()
    {
        return prefix + "/partner";
    }

    /**
     * 查询合作伙伴列表
     */
    @RequiresPermissions("system:partner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Partner partner)
    {
        startPage();
        List<Partner> list = partnerService.selectPartnerList(partner);
        return getDataTable(list);
    }

    /**
     * 导出合作伙伴列表
     */
    @RequiresPermissions("system:partner:export")
    @Log(title = "合作伙伴", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Partner partner)
    {
        List<Partner> list = partnerService.selectPartnerList(partner);
        ExcelUtil<Partner> util = new ExcelUtil<Partner>(Partner.class);
        return util.exportExcel(list, "partner");
    }

    /**
     * 新增合作伙伴
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存合作伙伴
     */
    @RequiresPermissions("system:partner:add")
    @Log(title = "合作伙伴", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Partner partner,String[] imgBannerUrl)
    {
        partner.setId(this.idGererateFactory.nextStringId());
        partner.setName(partner.getName());
        partner.setTitle(partner.getTitle());
        partner.setNotes(partner.getNotes());
        partner.setCreated(DateUtils.getNowDate());
        partner.setImg1Url(partner.getImg1Url());
        partner.setStatus(partner.getStatus());
        partner.setSort(partner.getSort());
        partnerService.insertPartner(partner);

        if(imgBannerUrl!=null){
            //合作伙伴图
            for (int i = 0; i < imgBannerUrl.length; i++) {
                RelevantImg relevantImg = new RelevantImg();
                relevantImg.setId(idGererateFactory.nextStringId());
                relevantImg.setImgUrl(imgBannerUrl[i]);
                relevantImg.setRelId(partner.getId());
                relevantImg.setType(0);
                relevantImg.setStatus(0);
                relevantImg.setSort(Integer.toUnsignedLong(i));
                this.relevantImgService.insertRelevantImg(relevantImg);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改合作伙伴
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Partner partner = partnerService.selectPartnerById(id);
        mmap.put("relevantImg",this.relevantImgService.findListByRelIdAndType(id,0));
        mmap.put("partner", partner);
        return prefix + "/edit";
    }

    /**
     * 修改保存合作伙伴
     */
    @RequiresPermissions("system:partner:edit")
    @Log(title = "合作伙伴", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Partner partner,String[] imgBannerUrl)
    {
        //删除数据库原本的相册资源
        this.relevantImgService.deleteByRelId(partner.getId());

        if(imgBannerUrl!=null){
            //合作伙伴图
            for (int i = 0; i < imgBannerUrl.length; i++) {
                RelevantImg relevantImg = new RelevantImg();
                relevantImg.setId(idGererateFactory.nextStringId());
                relevantImg.setImgUrl(imgBannerUrl[i]);
                relevantImg.setRelId(partner.getId());
                relevantImg.setType(0);
                relevantImg.setStatus(0);
                relevantImg.setSort(Integer.toUnsignedLong(i));
                this.relevantImgService.insertRelevantImg(relevantImg);
            }
        }
        return toAjax(partnerService.updatePartner(partner));
    }

    /**
     * 删除合作伙伴
     */
    @RequiresPermissions("system:partner:remove")
    @Log(title = "合作伙伴", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(partnerService.deletePartnerByIds(ids));
    }
}
